<?php

namespace App\Services\Blacklist;

use App\Dao\UserBlacklistDao;
use App\Exception\MessageException;
use App\Model\User;
use App\Model\UserBlacklist;

class BlacklistService
{

    /**
     * Note - 获取自己拉黑的用户
     * DateTime 2024/8/12 19:36
     * User Admin
     * @param mixed $uid
     * @return array
     */
    public function blackList(mixed $uid)
    {
        $model = UserBlacklistDao::getInstance();
        $list = $model->getBlacklist($uid);
        return formatData($list);
    }


    /**
     * Note - 设置、解除黑名单用户
     * 拉黑、解除拉黑
     * @param $uid
     * @param $black_uid
     * @param $type
     * @return array
     */
    public function setUserBlacklist($uid, $black_uid, $type)
    {
        // $black = $model->find("*", "uid=? and black_uid=?", [$uid, $black_uid]);
        $black = UserBlacklist::query()->where('black_uid', $black_uid)->where('uid', $uid)->first();
        if ($black) {
            //1:设置黑名单 2:当前拉黑的解除；被解除的,再次拉黑
            if ($type == 2) {
                // black_status: 0-拉黑 1-解除
                $updateData = $black['black_status'] == 1 ? ['black_status' => 0, 'black_time' => time()] : ['black_status' => 1];
            } else {
                $updateData = ['black_status' => 0, 'black_time' => time()];
            }
            $res = UserBlacklist::query()->where('id', $black['id'])->update($updateData);
        } else {
            $res = UserBlacklist::query()->insert([
                'uid' => $uid,
                'black_uid' => $black_uid,
                'black_time' => time()
            ]);
        }
        if ($res) {
            return formatData(['result' => 1, 'msg' => 'success']);
        }
        throw new MessageException('修改失败');
    }
}